package com.token.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @auther: henriy
 * @Date: 2023/11/1
 */
public class PatternUtils {

    /**
     * 匹配sql条件部分
     *
     * @param sqlCondition 条件表达式
     * @param column       参数值
     * @param value        参数字段
     * @return
     */
    public static String patternCondition(String sqlCondition, String column, String value) {
        String regex = "\\$\\{" + column + "\\}";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(sqlCondition);

        // 匹配到就替换，未匹配到就返回原值
        if (matcher.find()) {
            return matcher.replaceAll(value);
        }

        return sqlCondition;
    }

    public static void main(String[] args) {
        String str = patternCondition("daily.test_company_info.company_name like '%${company_name}%'", "名字", "company_name");
        System.out.println(str);
    }

}
